<?php

namespace App\Http\Controllers\Api;

use App\Models\Base;
use App\Models\Fang;
use App\Models\Fangattr;
use App\Models\Renting;
use Illuminate\Http\Request;

class FangController extends Base
{
    // Fang模型中获取推荐房源
    public function index(Request $request)
    {
        $page  = $request->get('page');
        $field = $request->get('field');
        $value = $request->get('idd');
        if ($field == 'a') {
            $data = Fang::paginate(5, ['*'], 'page', $page);
        } else {
            $data = Fang::where($field, $value)->paginate(5, ['*'], 'page', $page);
        }
        return $data;
    }

    public function getinfo(Request $request, int $fang)
    {
        $openid = $request->get('openid');
        $data   = Fang::where('id', $fang)->with('owner')->get();
        $favs   = Renting::where('openid', $openid)->value('fang_fav');
        $arr    = explode(',', $favs);
        if (in_array($fang, $arr)) {
            $data[0]['is_fav'] = 1;
        } else {
            $data[0]['is_fav'] = 0;
        }
        return $data;
    }

    // Fang模型中获取推荐房源
    public function tuijian()
    {
        $data = Fang::where('is_recommend', '1')->get();
        return $data;
    }

    // Fangattr 模型获取fang_group
    public function group(Request $request)
    {
        $name = $request->get('name');
        $pid  = Fangattr::where('field_name', $name)->value('id');
        $data = Fangattr::where('pid', $pid)->limit(4)->get();
        return $data;
    }

    // 房源收藏 fav
    public function fav(Request $request)
    {
        $openid  = $request->get('openid');
        $fang_id = $request->get('id');
        try {
            $fang_fav = Renting::where('openid', $openid)->value('fang_fav');
            $arr      = explode(',', $fang_fav);
            if (in_array($fang_id, $arr)) {
                return ['status' => 1009, 'msg' => '该房源已被收藏'];
            }
            $str = $fang_fav . ',' . $fang_id;
            Renting::where('openid', $openid)->update(['fang_fav' => $str]);
        } catch (\Exception $exception) {
            return ['status' => 1008, 'msg' => '收藏房源失败'];
        }
        return ['status' => 200, 'msg' => '收藏成功'];
    }

    //ES搜索
    public function esfang(Request $request)
    {
        // 获取查询关键字
        $kw = $request->get('keyword');
        if (empty($kw)) {
            return ['status' => 1009, 'msg' => '请输入查询条件'];
        }
        // 调用ES类来查询
        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('es.host'))->build();
        // 查询条件
        $params  = [
            'index' => 'fang',
            'type'  => '_doc',
            'body'  => [
                'query' => [
                    'match' => [
                        'fang_desn' => [
                            'query' => $kw
                        ]
                    ]
                ]
            ]
        ];
        $results = $client->search($params);
        $total   = $results['hits']['total'];
        if ($total > 0) {
            // 有数据
            $id_arr = array_column($results['hits']['hits'], '_id');
            $data   = Fang::whereIn('id', $id_arr)->paginate(5);
            return $data;
        }
        return ['status' => 1010, 'msg' => '未查询到相关信息'];
    }
}
